package com.yuan.model_community.rest;


import com.yuan.model_common.dto.CommonQueryCriteria;
import com.yuan.model_community.domain.CommunityFacilities;
import com.yuan.model_community.service.CommunityFacilitiesService;
import com.yuan.model_community.service.dto.CommunityFacilitiesDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.zhengjie.annotation.Log;
import me.zhengjie.utils.PageResult;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@RestController
@RequestMapping("/api/communityFacilities")
@RequiredArgsConstructor
@Api(tags = "物业便民: 小区设施管理")
public class CommunityFacilitiesController {

    private final CommunityFacilitiesService communityFacilitiesService;

    @ApiOperation("导出/community/facilities")
    @GetMapping(value = "/download")
    public void exportFacilities(HttpServletResponse response, CommonQueryCriteria criteria) throws IOException {
        communityFacilitiesService.download(communityFacilitiesService.queryAll(criteria), response);
    }

    @GetMapping("/findList")
    @Log("查询/api/community/facilities")
    @ApiOperation("查询/api/community/facilities")
    public ResponseEntity<PageResult<CommunityFacilitiesDto>> queryFacilitiesList(CommonQueryCriteria criteria, Pageable pageable) {
        PageResult<CommunityFacilitiesDto> communityNoticeDtoPageResult = communityFacilitiesService.queryAll(criteria, pageable);
        return new ResponseEntity<>(communityNoticeDtoPageResult, HttpStatus.OK);
    }


    @Log("新增/api/community/facilities")
    @ApiOperation("新增/api/community/facilities")
    @PostMapping("/create")
    public ResponseEntity<Object> createFacilities(@Validated @RequestBody CommunityFacilities resources) {
        communityFacilitiesService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @Log("修改/api/community/facilities")
    @ApiOperation("修改/api/community/facilities")
    @PostMapping("/update")
    public ResponseEntity<Object> updateFacilities(@Validated(CommunityFacilities.Update.class) @RequestBody CommunityFacilities resources) throws Exception {
        communityFacilitiesService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除/api/community/notice")
    @ApiOperation("删除/api/community/notice")
    @PostMapping("/delete")
    public ResponseEntity<Object> deleteFacilities(@RequestParam Integer ids) {
        communityFacilitiesService.delete(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

}
